package com.snailhd.cloud.account.authorization.filter;


import com.snailhd.cloud.account.authorization.exception.TokenException;
import com.snailhd.common.entity.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@ControllerAdvice
public class TokenExceptionHandler {

	Logger log = LoggerFactory.getLogger(this.getClass());

	@ExceptionHandler(value = TokenException.class)
	@ResponseBody
	public ResultVO handeAndReturnData(HttpServletRequest req, HttpServletResponse res, Exception exception) throws Exception{
		ResultVO re = new ResultVO();
		if(exception instanceof TokenException){
			TokenException te =   (TokenException)exception;
			re.generateFail();
			re.setSubCode(te.getErrorCode());
			re.setSubMsg(te.getErrorMsg());
			return re;
		}else {
			throw exception;
		}
	}
}
